<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/08/28 0028
 * Time: 9:57:46
 */
namespace app\admin\Controller;

class Shopcommentsta extends Datasta {

    //商家评论统计
    public function index(){
        //默认第一个商户
        $shop_id=1;
        //时间（x）
        $data= $this->settimes();
        $timelist=settime($data);
        $ordernum=[];
        $havecomment=[];
        $staravg=[];
        foreach ($timelist as $k=>$v){
            //订单数
            $where1['o_lasttime']=['between',[strtotime($v.' '.'00:00:00'),strtotime($v.' '.'23:59:59')]];
            $where1['o_status']=['egt',4];
            $where1['shop_id']=$shop_id;
            $orderlist=db('ordershop')
                ->join('ec_orderinfo','ec_orderinfo.id=ec_ordershop.order_id',left)
                ->where($where1)
                ->select();
            $ordernum[$k]=count($orderlist);
            //已评论订单数
            $where2['s_createtime']=['between',[strtotime($v.' '.'00:00:00'),strtotime($v.' '.'23:59:59')]];
            $where2['s_shopid']=['eq',$shop_id];
            $havecomment[$k]=db('shopremark')
                ->where($where2)
                ->count();
            //评论星级平均值
            $where3['s_shopid']=['eq',$shop_id];
            $where3['s_createtime']=['between',[strtotime($v.' '.'00:00:00'),strtotime($v.' '.'23:59:59')]];
            $staravg[$k]=db('shopremark')
                ->where($where3)
                ->avg('s_star');
        }

        $this->assign('title','商户评论统计');
        $this->assign('timelist',json_encode($timelist));
        $this->assign('ordernum',json_encode($ordernum));
        $this->assign('havecomment',json_encode($havecomment));
        $this->assign('staravg',json_encode($staravg));
        $this->assign('begin',date('Y-m-d',strtotime('-7day')));
        $this->assign('end',date('Y-m-d',time()));
        $this->assign('shop_ids',db('shop')->field('id,shop_name')->select());
        return $this->fetch();

    }
    //搜索
    public  function search(){
        $where['status']=1;
        $shop_id=input('shop_id')?input('shop_id'):1;
        //时间（x）
        $data= $this->settimes();
        $timelist=settime($data);
        $ordernum=[];
        $havecomment=[];
        $staravg=[];
        foreach ($timelist as $k=>$v){
            //订单数
            $where1['o_lasttime']=['between',[strtotime($v.' '.'00:00:00'),strtotime($v.' '.'23:59:59')]];
            $where1['o_status']=['egt',4];
            $where1['shop_id']=$shop_id;
            $orderlist=db('ordershop')
                ->join('ec_orderinfo','ec_orderinfo.id=ec_ordershop.order_id',left)
                ->where($where1)
                ->select();
            $ordernum[$k]=count($orderlist);
            //已评论订单数
            $where2['s_createtime']=['between',[strtotime($v.' '.'00:00:00'),strtotime($v.' '.'23:59:59')]];
            $where2['s_shopid']=['eq',$shop_id];
            $havecomment[$k]=db('shopremark')
                ->where($where2)
                ->count();
            //评论星级平均值
            $where3['s_shopid']=['eq',$shop_id];
            $where3['s_createtime']=['between',[strtotime($v.' '.'00:00:00'),strtotime($v.' '.'23:59:59')]];
            $staravg[$k]=db('shopremark')
                ->where($where3)
                ->avg('s_star');
        }
        $data['timelist']=$timelist;
        $data['ordernum']=$ordernum;
        $data['havecomment']=$havecomment;
        $data['staravg']=$staravg;
        return json($data);
    }


}